package com.bawei.module_recommend


import android.util.Log
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.core.view.get
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.Router
import com.bawei.lib_common.base.BaseToolbarActivity
import com.bawei.lib_common.base.state.Empty
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.observeWithLifecycle
import com.bawei.lib_net.storage.utils.Gson
import com.bawei.module_recommend.adapter.RecommendAdapter
import com.bawei.module_recommend.databinding.ActivityRecommendBinding
import com.bawei.module_recommend.ui.RecommendViewModel
import dagger.hilt.android.AndroidEntryPoint

@Route(path = Router.Ui.RecommendActivity)
@AndroidEntryPoint
class RecommendActivity : BaseToolbarActivity<RecommendViewModel, ActivityRecommendBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_recommend
    override val mViewModel: RecommendViewModel by viewModels()

    private val mAdapter: RecommendAdapter by lazy {
        RecommendAdapter(
            onItemClick = {
                ARouter.getInstance().build(Router.Ui.RecommendDetailActivity)
                    .withInt("id", it)
                    .navigation()
            }
        )
    }

    override fun initView() {
        mBinding.rvRecommend.adapter = mAdapter
        mBinding.rvRecommend.layoutManager = LinearLayoutManager(this)
    }

    override fun initData() {
        mViewModel.getMerchantList()
    }

    override fun initListener() {
        mViewModel.recommendPageFlow.observeWithLifecycle(this) {
            when (it) {
                is Success -> {
                    Log.d("TAG", "initListener: " + Gson.toJson(mViewModel.mRecommendList))
                    mAdapter.setData(mViewModel.mRecommendList)
                    mAdapter.notifyDataSetChanged()
                }

                Empty -> {}
                is Failed -> {}
                Loading -> {}
            }
        }
    }


    override fun initStatusBar() {
        mBinding.appbar.toolbar.inflateMenu(com.bawei.lib_common.R.menu.single_menu)
        mBinding.appbar.toolbar.menu[0].icon = ContextCompat.getDrawable(this, R.drawable.icon_add_black)
        mBinding.appbar.toolbar.setOnMenuItemClickListener {
            ARouter.getInstance().build(Router.Ui.SettledActivity).navigation()
            true
        }
        mBinding.appbar.toolbar.setNavigationOnClickListener {
            finish()
        }
    }

}